import $service from '../../../../service.js';
var util = require('../../../../utils/util.js')
var that;
var page = 1;
let app = getApp();
Page({
    data: {
        hisType:  $service.isHis,
        tab: 0,
        prescriptionlist: [],
        startdate: '',
        enddate: '',
        endtime: '',
        isshow: false,
        refreshLoading: false,
        loadingmore: false,
        noMore: false
    },
    onLoad(e) {},
    onShow() {
        that = this.data;
        page = 1;
        this.setData({
            noMore: false
        })
        this.getPatientPrescriptionList()
    },

    // 处方列表
    getPatientPrescriptionList() {
        let params = {
            "current": page,
            "startTime": that.startdate,
            "endTime": that.enddate,
            "isOverdue": Number(that.tab),
            "size": 10,
            "userId": JSON.parse($service.getStore('userInfo')).id
        }
        app.http.getPatientPrescriptionList(params).then(res => {
            if (page == 1) {
                that.prescriptionlist = res.data.list;
            } else {
                if (res.data.list.length !== 0) {
                    that.prescriptionlist = that.prescriptionlist.concat(res.data.list);
                } else {
                    this.setData({
                        noMore: true
                    })
                }
            }
            this.setData({
                prescriptionlist: that.prescriptionlist,
            })
        }).catch(err => {
            $service.showToast(err.msg);
        }).finally((res) => {
            setTimeout(() => {
                this.setData({
                    refreshLoading: false,
                    loadingmore: false
                })
            }, 1000)
        })
    },
    // 触底刷新
    loadmore() {
        if (that.prescriptionlist.length !== 0) {
            if (!that.noMore) {
                page++
                this.setData({
                    loadingmore: true
                })
                this.getPatientPrescriptionList();
            }
        }
    },
    // 下拉刷新
    dropdownRefresh() {
        page = 1;
        this.setData({
            refreshLoading: true,
            noMore: false
        })
        this.getPatientPrescriptionList();
    },
    // 选项卡
    taptab(e) {
        page = 1;
        var index = e.currentTarget.dataset.index;
        this.setData({
            tab: index
        })
        this.getPatientPrescriptionList();
    },
    //保存
    qr_Btn() {
        if (that.startdate) {
            if ($service.timeFormat(that.enddate) < $service.timeFormat(that.startdate)) {
                $service.showToast('结束时间不可小于开始时间')
                return
            }
        }
        page = 1;
        this.getPatientPrescriptionList();
        this.setData({
            isshow: false
        })
    },
    bookTime(e) {
        let type = e.currentTarget.dataset.type;
        let time = e.detail.time;
        if (type === 'start') {
            that.startdate = time
        } else {
            that.enddate = time
        }
        this.setData({
            startdate: that.startdate,
            enddate: that.enddate
        })
    },

    bindDateChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        var index = e.currentTarget.dataset.index;
        if (index == 1) {
            this.data.startdate = e.detail.value;
            startTime = this.data.startdate
        } else {
            this.data.enddate = e.detail.value;
            endTime = this.data.enddate
        }
        this.setData({
            startdate: this.data.startdate,
            enddate: this.data.enddate
        })
    },


    gotobuy(e) {
      var index = e.currentTarget.dataset.index;
      let {id, prescriptionNo, storeId} =  this.data.prescriptionlist[index]
      wx.navigateTo({
        url: '../../../prescription_flow/order_create/order_create?prescription_id=' + id + '&prescriptionNo=' + prescriptionNo + '&ishospitalPre=' + storeId
      })
    },

    gotopage(e) {
        var index = e.currentTarget.dataset.index;
        let data =  this.data.prescriptionlist[index]
        wx.navigateTo({
            url: '../../index/index_prescription_details/index_prescription_details?prescription_id=' + data.id +
                '&prescriptionNo=' + data.prescriptionNo + '&state=' + data.prescriptionState + '&paymentMethod=' + data.paymentMethod + '&ishospitalPre=' + data.storeId + '&inquiryType=' + data.inquiryType
        })
    },
    gotostop(e) {
        wx.showToast({
            title: '医生正在审核中，请稍后再查看',
            icon: 'none'
        })
    },
    gotoagain(e) {
        wx.showToast({
            title: '药师审核不通过',
            icon: 'none'
        })
    },
    //弹窗
    tapactive(e) {
        var num = e.currentTarget.dataset.num;
        if (num == 0) {
            this.data.isshow = !this.data.isshow
            this.setData({
                isshow: this.data.isshow
            })
        } else {
            var index = e.currentTarget.dataset.index;
            this.setData({
                activenum: index
            })
        }
    },
    clearTime(e) {
        var index = e.currentTarget.dataset.index;
        console.log(index)
        if (index == 1) {
            this.data.startdate = '';
        } else {
            this.data.enddate = ''
        }
        this.setData({
            startdate: this.data.startdate,
            enddate: this.data.enddate
        })
    },

})